//
//  CollectionWall.m
//  EFExtend
//
//  Created by mac on 15/4/16.
//  Copyright (c) 2015年 Eric. All rights reserved.
//

#import "CollectionWall.h"
#import "BookInfoCCell.h"

@interface CollectionWall ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collection;

@end

@implementation CollectionWall

- (void)dealloc
{
   
    HM_SUPER_DEALLOC();
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self.customNavLeftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.customNavLeftBtn setFrame:CGRectMakeBound(32, 32)];
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.backgroundColor = [UIColor md_amber_100];
    self.collection.alwaysBounceVertical = YES;
    [self.view addSubview:self.collection];
    [self.collection registerClass:[BookInfoCCell class] forCellWithReuseIdentifier:@"ImageTitleIdentifier"];
    
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-20);
        make.right.mas_equalTo(self.view.mas_right).offset(0);
    }];

    self.collection.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshing)];
    self.collection.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshing)];
    
    ((MJRefreshAutoFooter*)(self.collection.mj_footer)).automaticallyRefresh = YES;
    
    [self.collection.mj_header beginRefreshing];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

ON_Button(signal){
    UIButten *btn = signal.source;
    if ([signal is:[UIButten TOUCH_UP_INSIDE]]) {
        if ([btn is:@"leftBtn"]) {//customNavLeftBtn
            [self backAndRemoveWithAnimate:YES];
        }else if ([btn is:@"rightBtn"]){//customNavRightBtn
            
        }
    }
}

- (void)refreshing{
    [self delayInMain:0.1f invoke:^{
        [self.collection reloadData];
        
        if ([self.collection.mj_header isRefreshing]) {
            
            [self.collection.mj_header endRefreshing];
        }else{
            [self.collection.mj_footer endRefreshing];
        }
        
    }];
}


#pragma mark - collection delegate

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BookInfoCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageTitleIdentifier" forIndexPath:indexPath];
    if (indexPath.section==1) {
        
        cell.coverImageView.image = [UIImage imageNamed:@"back.png"];
        cell.name.text =@"test";
    }
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }
    return 4;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

#pragma mark - layout
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return CGSizeMake(collectionView.width, 30);
    }
    return CGSizeMake(80, 110);
}


@end
